package com.matt.repository.record;

import com.matt.domain.record.CheckRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/21
 * *******************************
 */
public interface CheckRecordRepository extends JpaRepository<CheckRecord, String> {
    CheckRecord findFirstByCheckInTimeBetweenAndChildrenIdAndCheckOutTimeIsNull(Date begin, Date end, String childrenId);

    CheckRecord findFirstByCheckInTimeBetweenAndChildrenId(Date begin, Date end, String childrenId);

    CheckRecord findFirstByCheckInTimeBetweenAndChildrenIdAndStatus(Date begin, Date end, String childrenId,Integer status);

    List<CheckRecord> findAllByCheckInTimeBetweenAndChildrenIdIn(Date begin, Date end, List<String> childrenIds);

    @Query("select c from CheckRecord c where c.childrenId=?1 and c.checkInTime>= ?2  order by c.checkInTime desc ")
    List<CheckRecord> findByChildrenIdAndCheckInTimeThan(String childrenId, Date today);

    @Query("select c from CheckRecord c where c.childrenId in ?1 and c.checkInTime>= ?2 order by c.checkInTime desc ")
    List<CheckRecord> findByChildrenIdInAndCheckInTimeGreaterThan(Collection<String> childrenIds, Date today);

    @Query("select c from CheckRecord c where c.childrenId=?1 order by c.checkInTime desc ")
    List<CheckRecord> findByChildrenId(String childrenId);

    @Query("select c from CheckRecord c where c.childrenId=?1 and c.created>= ?2  and c.created<= ?3 and c.height IS NOT NULL and c.weight IS NOT NULL order by c.created ")
    List<CheckRecord> findByChildrenIdAndCreatedBetween(String childrenId, Date beginDate, Date endDate);

    @Query(value = "select c.*  from check_record c where c.checkerId=?1  AND  DATE_FORMAT(c.created,'%Y-%m-%d') = DATE_FORMAT(?2,'%Y-%m-%d') )",nativeQuery = true)
    List<CheckRecord> findByCheckerIdAndToday(String checkerId, String today);

    @Query(value = "select c.*  from check_record c where c.childrenId=?1  and userType = ?2 ",nativeQuery = true)
    List<CheckRecord> findByChildrenIdAndTodayAndUserType(String childrenId, Integer userType);

    @Query(value = "select c.* from check_record c where c.childrenId in ?1  AND  DATE_FORMAT(c.created,'%Y-%m-%d') = DATE_FORMAT(?2,'%Y-%m-%d') order by  DATE_FORMAT(c.created,'%Y-%m-%d')  desc ",nativeQuery = true)
    List<CheckRecord> findByChildrenIdNotInAndCreated(List<String> childrenIds, Date today);

    @Query(value = "select * from check_record cr " +
            " where childrenId=?1 and checkInTime>=CONCAT(?2, ' 00:00')" +
            " and checkInTime < DATE_ADD(CONCAT(?2, ' 00:00'), INTERVAL 1 DAY)", nativeQuery = true)
    List<CheckRecord> findDayRecordByChildrenId(String childrenId, String day);

    @Query(value = "SELECT COUNT(DISTINCT childrenId) FROM `check_record` WHERE TO_DAYS(checkInTime) = TO_DAYS(:todayDate) and (status = 1 or status = 2 or status = 5 or status = 6) and classesId = :classesId", nativeQuery = true)
    Integer findRecordCountByClassId(@Param("todayDate") String todayDate, @Param("classesId") String classesId);

    // 原有方法：查询当天未出园的记录（保留，兼容其他逻辑）

    // 新增方法：查询当天某幼儿的入园类型记录（正常/迟到）
    List<CheckRecord> findByCheckInTimeBetweenAndChildrenIdAndStatusIn(
            Date begin, Date end, String childrenId, List<Integer> statusList
    );
}
